package com.wangtao.firstapp.activity;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.wangtao.firstapp.R;
import com.wangtao.firstapp.adapters.MessageBoardAdapter;
import com.wangtao.firstapp.modles.MessageBoardData;
import com.wangtao.firstapp.utils.HttpUtils;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;

/**
 * 留言板页面
 * Created by lanouhn on 16/9/10.
 */
public class MessageBoardActivity extends AppCompatActivity {
    private RecyclerView recyclerview_messageboard;
    private ImageView iv_back_titlebar_messageboard;
    private MessageBoardData messageBoardData;
    private MessageBoardAdapter messageBoardAdapter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_messageboard);
        initView();
        initData();
        initEvent();
    }

    private void initView() {
        recyclerview_messageboard = (RecyclerView) findViewById(R.id.recyclerview_messageboard);
        iv_back_titlebar_messageboard = (ImageView) findViewById(R.id.iv_back_titlebar_messageboard);
    }

    private void initData() {
        String url = "http://open4.bantangapp.com/users/message/list";
        String param = "app_id=com.jzyd.BanTang&client_id=bt_app_android&client_secret=ffcda7a1c4ff338e05c42e7972ba7b8d&track_user_id=2559956&oauth_token=6beffa8c92276d19d3c23acb596024e6&track_deviceid=866962021116243&track_device_info=m1+note&channel_name=sougou&app_installtime=1471923362&app_versions=5.8.8&os_versions=5.1&screensize=1080&v=19&page=0&pagesize=20";
        try {
            HttpUtils.doPostAsyn(url, param, new HttpUtils.CallBack() {
                @Override
                public void onRequestComplete(String result) {
                    Gson gson = new Gson();
                    messageBoardData = gson.fromJson(result, MessageBoardData.class);
                    if (messageBoardData.getData().getList().size() > 0) {
                        handler.sendEmptyMessage(1);
                    } else {

                    }
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void initEvent() {
        iv_back_titlebar_messageboard.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 0:
                    Toast.makeText(MessageBoardActivity.this, "网络访问失败", Toast.LENGTH_SHORT).show();
                    break;
                case 1:
                    messageBoardAdapter = new MessageBoardAdapter(MessageBoardActivity.this, messageBoardData);
                    recyclerview_messageboard.setAdapter(messageBoardAdapter);
                    LinearLayoutManager linearLayoutManager = new LinearLayoutManager(MessageBoardActivity.this, LinearLayoutManager.VERTICAL, true);
                    recyclerview_messageboard.setLayoutManager(linearLayoutManager);
                    break;
            }
        }
    };

}
